package com.lb.jwitter.action;


import java.util.ArrayList; 
import java.util.List; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblDKMHKetqua;
import com.lb.jwitter.service.TblDKMHKetquaService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHKetquaSearchAction{
    private int pageSize; 
    public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
     }

    public int getPageSize() {
       return pageSize;
 }

    private int page; 
    public void setPage(int page) { this.page = page;   }

    public int getPage() {
       return page;
   }

    private int pages; 
    public void setPages(int pages) { this.pages = pages;   }

    public int getPages() {
       return pages;
     }

    private String dataString; 
    public void setDataString(String dataString) { this.dataString = dataString;    }

    public String getDataString() {
       return dataString;
    }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int dotDKId; 
    public void setDotDKId(int dotDKId) {
       this.dotDKId = dotDKId;
  }

    public int getDotDKId() {
       return dotDKId;
  }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
  }

    public int getHocVienId() {
       return hocVienId;
  }

    private int nLMHId; 
    public void setNLMHId(int nLMHId) {
       this.nLMHId = nLMHId;
  }

    public int getNLMHId() {
       return nLMHId;
  }

    private String ketQua; 
    public void setKetQua(String ketQua) {
       this.ketQua = ketQua;
   }

    public String getKetQua() {
       return ketQua;
   }

    private String tungayDangKy; 
    public void setTuNgayDangKy(String tungayDangKy) {
       this.tungayDangKy = tungayDangKy;
   }

    public String getTuNgayDangKy() {
       return tungayDangKy;
   }

    private String denngayDangKy; 
    public void setDenNgayDangKy(String denngayDangKy) {
       this.denngayDangKy = denngayDangKy;
   }

    public String getDenNgayDangKy() {
       return denngayDangKy;
   }

    private String tungayCapNhat; 
    public void setTuNgayCapNhat(String tungayCapNhat) {
       this.tungayCapNhat = tungayCapNhat;
   }

    public String getTuNgayCapNhat() {
       return tungayCapNhat;
   }

    private String denngayCapNhat; 
    public void setDenNgayCapNhat(String denngayCapNhat) {
       this.denngayCapNhat = denngayCapNhat;
   }

    public String getDenNgayCapNhat() {
       return denngayCapNhat;
   }

    private int xacNhanYN; 
    public void setXacNhanYN(int xacNhanYN) {
       this.xacNhanYN = xacNhanYN;
  }

    public int getXacNhanYN() {
       return xacNhanYN;
  }

    private String tinhTrang; 
    public void setTinhTrang(String tinhTrang) {
       this.tinhTrang = tinhTrang;
   }

    public String getTinhTrang() {
       return tinhTrang;
   }
    
    private String hoTen;
    public String getHoTen() {
		return hoTen;
	}

	public void setHoTen(String hoTen) {
		this.hoTen = hoTen;
	}

	private String mssv;

    public String getMssv() {
		return mssv;
	}

	public void setMssv(String mssv) {
		this.mssv = mssv;
	}

	private List<TblDKMHKetqua> tblDKMHKetquas; 
    public List<TblDKMHKetqua>  getTblDKMHKetquas() {
        return tblDKMHKetquas; 
      } 

    private TblDKMHKetquaService tblDKMHKetquaService; 

	private int preSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postSearch(Message msg, List<ConditionReport> cons ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
	
	private String maNhomLop;
  public String getMaNhomLop() {
		return maNhomLop;
	}

	public void setMaNhomLop(String maNhomLop) {
		this.maNhomLop = maNhomLop;
	}
	
	private String maMonHoc;

public String getTungayDangKy() {
		return tungayDangKy;
	}

	public void setTungayDangKy(String tungayDangKy) {
		this.tungayDangKy = tungayDangKy;
	}

	public String getDenngayDangKy() {
		return denngayDangKy;
	}

	public void setDenngayDangKy(String denngayDangKy) {
		this.denngayDangKy = denngayDangKy;
	}

	public String getTungayCapNhat() {
		return tungayCapNhat;
	}

	public void setTungayCapNhat(String tungayCapNhat) {
		this.tungayCapNhat = tungayCapNhat;
	}

	public String getDenngayCapNhat() {
		return denngayCapNhat;
	}

	public void setDenngayCapNhat(String denngayCapNhat) {
		this.denngayCapNhat = denngayCapNhat;
	}

public String getMaMonHoc() {
		return maMonHoc;
	}

	public void setMaMonHoc(String maMonHoc) {
		this.maMonHoc = maMonHoc;
	}
private int xacNhanYNAll;
public int getXacNhanYNAll() {
	return xacNhanYNAll;
}

public void setXacNhanYNAll(int xacNhanYNAll) {
	this.xacNhanYNAll = xacNhanYNAll;
}

private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_Ketqua", "xem")){ 
        return "noprivilege"; 
    }
        tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient"); 
        msg = new Message(); 
        
    
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
    if(dotDKId != 0 ){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("dotDKId.id");
        con.setValue1(new Long(dotDKId));
        con.setOperator("eq");
        cons.add(con);	
    }else{
    	dataString = ""; 
        dataString += "&dotDKId=" + dotDKId; 
        dataString += "&mssv=" + mssv; 
        dataString += "&maNhomLop=" + maNhomLop; 
        dataString += "&maMonHoc=" + maMonHoc; 
        dataString += "&ketQua=" + ketQua; 
        dataString += "&tungayDangKy=" + tungayDangKy; 
        dataString += "&denngayDangKy=" + denngayDangKy; 
        dataString += "&tungayCapNhat=" + tungayCapNhat; 
        dataString += "&denngayCapNhat=" + denngayCapNhat; 
        dataString += "&xacNhanYN=" + xacNhanYN; 
        dataString += "&tinhTrang=" + tinhTrang; 
        return Action.SUCCESS; 
    }
    if(xacNhanYN != -1){
	    if(xacNhanYN == 1 ){ 
	        ConditionReport con = new ConditionReport(); 
	        con.setCol("xacNhanYN");
	        con.setValue1(true);
	        con.setOperator("eq");
	        cons.add(con);	
	    }else{
	    	ConditionReport con = new ConditionReport(); 
	        con.setCol("xacNhanYN");
	        con.setValue1(false);
	        con.setOperator("eq");
	        cons.add(con);	
	    }
    }
    if(mssv != null && !mssv.equals("")){
   	 ConditionReport con = new ConditionReport(); 
         con.setCol("hocVienId.MSSV");
        con.setValue1("%" + mssv + "%");
        con.setOperator("like");
        cons.add(con);
   }
    if(maNhomLop != null && !maNhomLop.equals("")){
   	 ConditionReport con = new ConditionReport(); 
         con.setCol("NLMHId.ma");
        con.setValue1("%" + maNhomLop + "%");
        con.setOperator("like");
        cons.add(con);
   }
    if(maMonHoc != null && !maMonHoc.equals("")){
   	 ConditionReport con = new ConditionReport(); 
         con.setCol("NLMHId.monHocId.ma");
        con.setValue1("%" + maMonHoc + "%");
        con.setOperator("like");
        cons.add(con);
   }


    if(ketQua != null && !ketQua.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("ketQua");
        con.setValue1("%" + ketQua + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
    if( tungayDangKy != null && !tungayDangKy.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayDangKy");
        con.setValue1(tungayDangKy);
        if (denngayDangKy != null && !denngayDangKy.equalsIgnoreCase("") ){ 
            con.setValue2(denngayDangKy); 
            con.setOperator("between"); 
        } 
        else{ 
            con.setOperator("ge"); 
        } 
        cons.add(con);	
    } 
        else if (denngayDangKy != null && !denngayDangKy.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayDangKy");
        con.setValue1(denngayDangKy);
        con.setOperator("le");
        cons.add(con);	
    } 
    if( tungayCapNhat != null && !tungayCapNhat.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayCapNhat");
        con.setValue1(tungayCapNhat);
        if (denngayCapNhat != null && !denngayCapNhat.equalsIgnoreCase("") ){ 
            con.setValue2(denngayCapNhat); 
            con.setOperator("between"); 
        } 
        else{ 
            con.setOperator("ge"); 
        } 
        cons.add(con);	
    } 
        else if (denngayCapNhat != null && !denngayCapNhat.equalsIgnoreCase("") ){ 
        ConditionReport con = new ConditionReport(); 
        con.setType("date"); 
         con.setCol("ngayCapNhat");
        con.setValue1(denngayCapNhat);
        con.setOperator("le");
        cons.add(con);	
    } 
    if(tinhTrang != null && !tinhTrang.equalsIgnoreCase("")){ 
        ConditionReport con = new ConditionReport(); 
        con.setCol("tinhTrang");
        con.setValue1("%" + tinhTrang + "%");
        con.setOperator("like");
        cons.add(con);	
    } 
        preSearch(msg, cons); 
       if (msg.isPreMessage_b()) 
       { 
           if(pageSize == 0){ 
               pageSize = 20; 
           } 
            if(page == 0){ 
               page =1; 
           } 
           tblDKMHKetquas = tblDKMHKetquaService.findtblDKMHKetquaByCondition(cons,pageSize,page); 
           pages = tblDKMHKetquaService.counttblDKMHKetquaByCondition(cons)/pageSize + 1; 
           postSearch(msg, cons); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 
      dataString = ""; 
      dataString += "&dotDKId=" + dotDKId; 
      dataString += "&mssv=" + mssv; 
      dataString += "&maNhomLop=" + maNhomLop; 
      dataString += "&maMonHoc=" + maMonHoc; 
      dataString += "&ketQua=" + ketQua; 
      dataString += "&tungayDangKy=" + tungayDangKy; 
      dataString += "&denngayDangKy=" + denngayDangKy; 
      dataString += "&tungayCapNhat=" + tungayCapNhat; 
      dataString += "&denngayCapNhat=" + denngayCapNhat; 
      dataString += "&xacNhanYN=" + xacNhanYN; 
      dataString += "&tinhTrang=" + tinhTrang; 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Seach tbl_DKMH_Ketqua Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
